package com.lanou.utp.controller;

import com.lanou.utp.entity.ResponseData;
import com.lanou.utp.entity.Teachlink;
import com.lanou.utp.service.TeachingServcie;
import com.lanou.utp.utils.CalendarUtils;
import com.lanou.utp.utils.IdAndName;
import com.lanou.utp.utils.TeachLinkQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author MaQingYun
 */
@Api("教学")
@RestController
public class TeachingController {

    @Value("${upload.path}")
    private String uploadPath;

    @Resource
    private TeachingServcie teachingServcie;

    @PostMapping("/resource/file")
    public ResponseData uploadFile(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        file.transferTo(new File(this.uploadPath));
        String url = this.uploadPath + originalFilename;
        return new  ResponseData("ok",url);
    }

    @PostMapping("/teachlinks")
    public ResponseData getTeachlinks(@RequestBody TeachLinkQuery query) {
        //TODO 需要获取当前用户的majorid
        long majorId = 1L;
        query.setMajorId(majorId);
        Integer searchdateId = query.getSearchdate();
        //根据不同的搜索ID, 获取开始日期和结束日期
        if (searchdateId != null && searchdateId != 0){
            HashMap<String, Date> calendar = CalendarUtils.teachingCalendar(searchdateId);
            query.setSearchStartDate(calendar.get(CalendarUtils.SEARCH_START_DATE));
            query.setSearchEndDate(calendar.get(CalendarUtils.SEARCH_END_DATE));
        }
        return this.teachingServcie.getTeachlinks(query);

    }

    @PostMapping("/teachlink")
    public ResponseData addTeachingLink(@RequestBody Teachlink teachlink) {
        long majorId = 1L;
        //TODO 需要获取当前用户的majorid
        teachlink.setMajorId(majorId);
        this.teachingServcie.addTeachingLink(teachlink);
        return new ResponseData("ok");
    }

    @ApiOperation("新增教学任务,根据教学Id获取老师")
    @GetMapping("/option/teachlink/{id}/teachers")
    public ResponseData getTeachlinkTeachers (@PathVariable Long id) {
        List<IdAndName> teachLinkTeachers = this.teachingServcie.getTeachlinkTeachers (id);
        return new ResponseData(teachLinkTeachers);
    }

    @ApiOperation("发布教学环节")
    @PostMapping("/teachlink/{id}/on")
    public ResponseData updateTeachlinkStatus(@PathVariable Long id) {
        this.teachingServcie.updateTeachlinkStatus(id);
        return new ResponseData("ok");
    }

    @ApiOperation("根据教学id,查询教学环节,准备修改内容")
    @GetMapping("/teachlink/{id}")
    public ResponseData getTeachlinkById(@PathVariable Long id){
        Teachlink teachlink = this.teachingServcie.getTeachlinkById(id);
        return new ResponseData(teachlink);
    }

    /**
     * 更新教学环节
     */
    @PutMapping("/teachlink")
    public ResponseData updateTeachlink(@RequestBody Teachlink teachlink){
        this.teachingServcie.updateTeachlink(teachlink);
        return new ResponseData("ok");
    }

    @DeleteMapping("/teachlink/{id}")
    public ResponseData deleteTeachlinkId(@PathVariable Long id) {
        this.teachingServcie.deleteTeachinkById(id);
        return new ResponseData("ok");
    }


}
